package com.opengl.test.opengltest.objects;

import com.opengl.test.opengltest.data.VertexArray;
import com.opengl.test.opengltest.programs.TextureShaderProgram;

import static android.opengl.GLES20.GL_TRIANGLE_FAN;
import static android.opengl.GLES20.glDrawArrays;
import static com.opengl.test.opengltest.Constants.BYTES_PER_FLOAT;

/**
 * Created by Cuckoo322 on 4/11/2017.
 */

public class Table {

    //事实常量：每个顶点包含2个分量
    private static final int POSITION_COMPONENT_COUNT = 2;
    private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;

    //读取点时在内存中进行指针跳跃使用的步幅（跨距），以字节为单位！
    private static final int STRIDE = (POSITION_COMPONENT_COUNT + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT;

    //桌子顶点数据
    private static final float[] VERTEX_DATA = {
            //分量顺序：x, y, s, t
            //三角扇
            //此处的t在定义的时候即对图像做了剪裁，取0.1~0.9这部分
            0f,    0f, 0.5f, 0.5f,
            -0.5f, -0.8f, 0f, 0.9f,
            0.5f,  -0.8f, 1f, 0.9f,
            0.5f,  0.8f, 1f, 0.1f,
            -0.5f, 0.8f, 0f, 0.1f,
            -0.5f, -0.8f, 0f, 0.9f
    };

    private final VertexArray vertexArray;

    public Table() {
        this.vertexArray = new VertexArray(VERTEX_DATA);
    }

    public void bindData(TextureShaderProgram textureShaderProgram) {
        //设置获取顶点数据的指针
        vertexArray.setVertexAttribPointer(
                0,
                textureShaderProgram.getPositionAttributeLocation(),
                POSITION_COMPONENT_COUNT,
                STRIDE
        );

        //设置获取纹理坐标数据的指针
        vertexArray.setVertexAttribPointer(
                POSITION_COMPONENT_COUNT,
                textureShaderProgram.getTextureCoordinatesAttributeLocation(),
                TEXTURE_COORDINATES_COMPONENT_COUNT,
                STRIDE
        );
    }

    public void draw() {
        glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
    }
}
